//
//  DateView.m
//  ChatDemo-UI2.0
//
//  Created by li.guibin on 15/12/15.
//  Copyright © 2015年 li.guibin. All rights reserved.
//

#import "DateView.h"
@interface DateView()
{
//    UIDatePicker * datePicker;
    UIControl *backView;
    UIView *datePickerView;
    
    int _type;
}

@end

@implementation DateView
@synthesize datePicker;
- (instancetype)initAddSuperView:(UIView *)superView canceltype:(int)type
{
    self = [super init];
    
    if (self)  {
        
        _type = type;
        
        backView = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
        backView.alpha = 0.5;
        backView.backgroundColor = [UIColor redColor];
        [backView setBackgroundColor:[UIColor colorWithPatternImage: [UIImage imageNamed:@"toumingbg"]]];
        [backView addTarget:self action:@selector(cancelControl) forControlEvents:UIControlEventTouchUpInside];
        [superView.window addSubview:backView];
        
        self.userInteractionEnabled = YES;
        self.frame = CGRectMake(0, SCREENHEIGH -200, SCREENWIDTH, 200);
        self.backgroundColor=[UIColor whiteColor];
        
        datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, self.frame.size.width, self.frame.size.height-44)];
        datePicker.backgroundColor=[UIColor whiteColor];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [self addSubview:datePicker];
        
        
        UIButton * ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [ensureBtn setFrame:CGRectMake(self.frame.size.width -44 - 15, 0, 50, 44)];
        [ensureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [ensureBtn setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
        [ensureBtn addTarget:self action:@selector(ensureAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:ensureBtn];
        
        UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setFrame:CGRectMake(15, 0, 50, 44)];
        [cancelBtn setTitle:_type == 1 ? @"取消" : @"清空" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        

            
        [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self addSubview:cancelBtn];
        
        [superView.window addSubview:self];
        
    }

    return self;
}
- (void)ensureAction{
    self.isUp = YES;
    if (_getDate != NULL) {
        NSString * dateStr = [Utils getStringByDate:datePicker.date];
        self.getDate(dateStr);
    }
    [backView removeFromSuperview];
    [self removeFromSuperview];
    [_delegate loadDateSource:self date:[Utils getStringByDate:datePicker.date]];
    if ([_aDelegate respondsToSelector:@selector(changeArrowDirection:)]) {
    [_aDelegate changeArrowDirection:self.isUp];
    }

}
- (void)cancelAction{
    
    if (_type == 1) {
        self.isUp = YES;
        [backView removeFromSuperview];
        [self removeFromSuperview];
        if ([_aDelegate respondsToSelector:@selector(changeArrowDirection:)])
        {
            [_aDelegate changeArrowDirection:self.isUp];
        }
    }else
    {
        self.getDate(nil);
        [backView removeFromSuperview];
        [self removeFromSuperview];

    }
}

- (void)cancelControl
{
    self.isUp = YES;
    [backView removeFromSuperview];
    [self removeFromSuperview];
    if ([_aDelegate respondsToSelector:@selector(changeArrowDirection:)]) {
        
        [_aDelegate changeArrowDirection:self.isUp];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
